package com.china08.yxyapi.api.manage.tec.teaching.continuedu.mytask;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTaskLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskLessonItemRepository;

/**
 * 
 * @annotation: 继续教育-课时任务进度修改
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyMyLessonItemProgressUpdController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduUserTaskLessonItemRepository yxyContinueduUserTaskLessonItemRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/item/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        Long progress = reqModel.progress;
        Long totalTime = reqModel.totalTime;
        YxyContinueduUserTaskLessonItem userTaskLessonItem = yxyContinueduUserTaskLessonItemRepository
                .findById(reqModel.itemId);
        RespModel model = new RespModel();
        if (userTaskLessonItem != null && userTaskLessonItem.getStatus() != 1) {
            userTaskLessonItem.setCurrentView(progress);
            yxyContinueduUserTaskLessonItemRepository.save(userTaskLessonItem);
            // 获取课时时长 判断是否已完成百分之80的进度
            double rate = (float) progress / totalTime * 100;
            if (rate > 80) {
                model.isFinish = 1;
            } else {
                model.isFinish = 0;
            }
        }
        model.setMessage("修改课时进度成功");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 课时ID
        public String itemId;
        // 课时进度
        public Long progress;
        // 课时总时长
        public Long totalTime;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 0 没有完成 1 完成了
        public Integer isFinish;
    }
}
